<template>
	<custom-layout custom-class="custom-layout">
		<!-- 表单 -->
		<wd-form ref="formRef" :model="formData">
			<wd-picker
				:columns="teamUserOptions" 
				label="施工班组" 
				v-model="formData.teamUserId" 
		  		:rules="[
				    { required: true, message: '请选择开工时间' },
				]" 
				value-key="id" 
  				label-key="name"
				@confirm="handleConfirm" />
		</wd-form>
		<!-- 提交 -->
		<wd-button block size="large" custom-class="submit-button" @click="handleSubmit">提交</wd-button>
		
	</custom-layout>
</template>

<script setup>
	import {
		reactive,
		ref
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		useDictionaryStore,
		useUserStore
	} from '@/store/index.js'
	import {
		updateTeamEngineeringStage,
		getEngineeringTeamUsers
	} from '@/api/engineering/detail.js'
	import dayjs from 'dayjs'

	// 字典仓库
	const dictionaryStore = useDictionaryStore()
	// 用户仓库
	const userStore = useUserStore()
	
	// 表单ref
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	// 施工班组option
	const teamUserOptions = ref([])
	const getTeamUserOptionList = (engineeringNode,companyId) => {

  		const params = {
  		  pageSize: -1,
  		  jobType: engineeringNode,
  		  companyId: companyId
  		}
  		getEngineeringTeamUsers(params).then(data => {
  		  teamUserOptions.value = data.list
  		}).catch((error) => {
    	uni.showToast({
    		title: error,
    		icon: 'error'
    	})
    })
}

	onLoad((option)=>{
		const item = JSON.parse(decodeURIComponent(option.row))
		formData.value = item
		getTeamUserOptionList(item.engineeringNode,item.companyId)
	})
	// 处理提交
	const handleSubmit = async () => {
		const validateResult = await formRef.value.validate()
		if (!validateResult.valid) return
		uni.showLoading({
			mask: true
		})
		const params = {
			...formData.value,
			
		}
		
		updateTeamEngineeringStage(params).then(()=>{
			uni.navigateBack(); // 返回上一页
			uni.$emit('refresh-confirm-complete'); // 触发刷新线索列表
		}).catch((error) => {
			console.log(error);
			
			setTimeout(() => {
				uni.showToast({
					title: '提交失败',
					icon: 'error'
				})
			})
		}).finally(() => {
			uni.hideLoading()
			
		})
		
	}
	const handleConfirm = ({ value, prop }) => {
//   if (value) {
//     formData[prop] = dayjs(value).format('YYYY-MM-DD 23:59:59');
//   } else {
//     formData[prop] = undefined;
//   }
};
	
</script>

<style lang="scss" scoped>
	// 自定义布局
	:deep(.custom-layout) {
		background: #F8F8F8;
	}

	// 提交按钮
	:deep(.submit-button) {
		margin: 32rpx;
	}
	.cell-title {
		font-size: 30rpx;
		font-weight: bold;
		margin: 24rpx;
		padding-left: 10rpx;
		position: relative;
	}
	.cell-title:before {
		content: "";
		position: absolute;
		left: -2px;
		top: 15%;
		height: 70%;
		width: 6rpx;
		background-color: #6a9cff;
	}
</style>